package com.berte.uart;

import jssc.SerialPort;
import jssc.SerialPortException;
import jssc.SerialPortList;

import java.util.Arrays;

/**
 * 用于管理一个串口
 */
public class Uart {
    public static final int PARITY_NONE = 0;
    public static final int PARITY_ODD = 1;
    public static final int PARITY_EVEN = 2;
    public static final int PARITY_MARK = 3;
    public static final int PARITY_SPACE = 4;

    private String[] porNames = null;
    private SerialPort serialPort = null;

    private int baudRate = 115200;
    private int dataBits = 8;
    private int stopBits = 1;
    private int parity = PARITY_NONE;
    private boolean setRTS = false;
    private boolean setDTR = true;

//    private boolean readLock = false;
//    private boolean writeLock = false;

    /**
     * 列出计算机中的所有的串口名
     *
     * @return 所有的串口名
     */
    public String[] getUartlist() {
        porNames = SerialPortList.getPortNames();
        return porNames;
    }

    /**
     * 打开串口
     *
     * @param name 串口名
     * @return 0：成功
     * -1：失败
     */
    public boolean open(String name) {
        if (Arrays.binarySearch(porNames, name) < 0) {
            return false;
        }
        serialPort = new SerialPort(name);

        try {
            //打开串口
            serialPort.openPort();
            //设置参数
            serialPort.setParams(baudRate, dataBits, stopBits, parity, setRTS, setDTR);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean close() {
        try {
            return serialPort.closePort();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 设置串口参数
     *
     * @param baudRate 波特率
     * @param dataBits 数据位
     * @param stopBits 停止位
     * @param parity   校验
     * @param setRTS   硬件控制
     * @param setDTR   硬件控制
     */
    public void setParams(int baudRate, int dataBits, int stopBits, int parity, boolean setRTS, boolean setDTR) {
        this.baudRate = baudRate;
        this.dataBits = dataBits;
        this.stopBits = stopBits;
        this.parity = parity;
        this.setRTS = setRTS;
        this.setDTR = setDTR;
    }

    public int getBaudRate() {
        return baudRate;
    }

    public void setBaudRate(int baudRate) {
        this.baudRate = baudRate;
    }

    public int getDataBits() {
        return dataBits;
    }

    public void setDataBits(int dataBits) {
        this.dataBits = dataBits;
    }

    public int getStopBits() {
        return stopBits;
    }

    public void setStopBits(int stopBits) {
        this.stopBits = stopBits;
    }

    public int getParity() {
        return parity;
    }

    public void setParity(int parity) {
        this.parity = parity;
    }

    public boolean isSetRTS() {
        return setRTS;
    }

    public void setSetRTS(boolean setRTS) {
        this.setRTS = setRTS;
    }

    public boolean isSetDTR() {
        return setDTR;
    }

    public void setSetDTR(boolean setDTR) {
        this.setDTR = setDTR;
    }

    public boolean writeBytes(byte[] buffer) {
/*        if (writeLock == true) {
            return false;
        }

        writeLock = true;*/

        try {
            if (!serialPort.writeBytes(buffer)) {
//                writeLock = false;
                return false;
            }
//            writeLock = false;
            return true;
        } catch (Exception e) {
            e.printStackTrace();
//            writeLock = false;
            return false;
        }
    }

    public byte[] readBytes(int byteCount) {
        try {
            return serialPort.readBytes(byteCount);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public int getInputBufferBytesCount() {
        try {
            return serialPort.getInputBufferBytesCount();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
